package com.lanou.sfu.songforu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.datas.RecommendPlayDetailDatas;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by quyan on 15/10/24.
 */
public class RecommendViewAdapter extends BaseAdapter {
    private List<RecommendPlayDetailDatas.ContentEntity> contentEntities;
    private Context context;
    private MainActivity activity;

    public RecommendViewAdapter(Context context,List<RecommendPlayDetailDatas.ContentEntity> dataes) {
        this.context = context;
        contentEntities = dataes;
        activity = (MainActivity) context;

    }

    @Override
    public int getCount() {
        return contentEntities != null && contentEntities.size() > 0 ? contentEntities.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return contentEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.listview_view_item,null);
            holder = new ViewHolder();
            holder.authername = (TextView) convertView.findViewById(R.id.autherNames);
            holder.songname = (TextView) convertView.findViewById(R.id.songNames);
            holder.itemView = (LinearLayout) convertView.findViewById(R.id.itemView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        RecommendPlayDetailDatas.ContentEntity contentEntity = (RecommendPlayDetailDatas.ContentEntity) getItem(position);
        holder.authername.setText(contentEntity.getAuthor());
        holder.songname.setText(contentEntity.getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MusicListDatas> lists = new ArrayList<>();
                for (RecommendPlayDetailDatas.ContentEntity entity : contentEntities) {
                    MusicListDatas musicList = new MusicListDatas();
                    musicList.setName(entity.getTitle());
                    musicList.setSinger(entity.getAuthor());
                    musicList.setSongId(entity.getSong_id());
//                    musicList.setSingerImage(entity.get);
                    musicList.setFrom(context.getString(R.string.fromnet));
                    lists.add(musicList);
//                    //发布
//                    MyEvent.MusicDatasEvent datasEvent = new MyEvent.MusicDatasEvent();
//                    datasEvent.setListDatases(lists);
//                    EventBus.getDefault().post(datasEvent);

                }
                MainActivity.play(lists, position);
            }
        });
        return convertView;
    }
    public class ViewHolder{

        private TextView songname,authername;
        private LinearLayout itemView;
    }
}
